package com.example.wuyeguanli.tools;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.example.wuyeguanli.R;
import com.example.wuyeguanli.entiy.Facility;

import java.util.List;

public class FacilityAdapter extends ArrayAdapter<Facility> {

    public FacilityAdapter(@NonNull Context context, List<Facility> facilities) {
        super(context, 0, facilities);
    }

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        return createViewFromResource(position, convertView, parent, android.R.layout.simple_spinner_item);
    }

    @Override
    public View getDropDownView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        return createViewFromResource(position, convertView, parent, android.R.layout.simple_spinner_dropdown_item);
    }

    private View createViewFromResource(int position, @Nullable View convertView, @NonNull ViewGroup parent, int resource) {
        if (convertView == null) {
            convertView = LayoutInflater.from(getContext()).inflate(resource, parent, false);
        }

        TextView textView = convertView.findViewById(android.R.id.text1);
        Facility facility = getItem(position);

        if (facility != null) {
            textView.setText(facility.getFacilityName());
        }

        return convertView;
    }
}